//------------------------------------------------------------------------
//
//	CCGameBox is a game development library for iPhone and iPod Touch.
//
//	Licensed under the BSD license, see LICENSE in root for details.
// 
//	Copyright (c) 2009 James Hui (a.k.a. Dr.Watson)
// 
//	For latest updates, please visit http://www.cocoachina.com
//
//------------------------------------------------------------------------

#import "MainViewController.h"
#import "EAGLView.h"


@implementation MainViewController

@synthesize glView;


static MainViewController *sharedInstance;

+ (MainViewController *) getInstance 
{
	return sharedInstance;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	    [super viewDidLoad];
	//motionManager = [[CMMotionManager alloc] init]; 
//	
//	if (motionManager.deviceMotionAvailable) {
//		[motionManager startGyroUpdates];
//		//motionManager.gyroUpdateInterval = 1.0/60.0;
//		[motionManager startDeviceMotionUpdates];
//		//motionManager.deviceMotionUpdateInterval = 30.0/60.0;
//    //[motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
//		//                               withHandler: ^(CMGyroData *gyroData, NSError *error)
//		//		 {
//		//			 CMAttitude *rotate = motionManager.deviceMotion.attitude;
//		//			// CMRotationRate rotate = gyroData.rotationRate;
//		//			 
//		//			 NSLog(@"rotation rate = [x:%f\n, y:%f\n, z:%f\n]",rotate.roll ,rotate.pitch, rotate.yaw);
//		//		 }];	} else {
//		//		NSLog(@"No gyroscope on device.");
//		//		[motionManager release];
//	}
//	
	sharedInstance = self;
	lastRoll = 0.0;
	glView.animationInterval = 1.0 / 60.0;
	[glView startAnimation];
//	[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(deviceMotionUpdate) userInfo:nil repeats:YES];
}

- (void)deviceMotionUpdate
{
	//CMRotationRate rotate = motionManager.deviceMotion.rotationRate;
//	NSLog(@"rotation rate = [x:%f\n, y:%f\n, z:%f\n]",rotate.x ,rotate.y, rotate.z);
	CMAttitude *attitude = motionManager.deviceMotion.attitude;
//	CMRotationRate rotate1 = motionManager.gyroData.rotationRate;
//	//			 
//	NSLog(@"rotation rate1 = [x:%f\n, y:%f\n, z:%f\n]",rotate1.x ,rotate1.y, rotate1.z);
//	CMAttitude *rotate2 = motionManager.deviceMotion.attitude;
	NSLog(@"rotation rate2 = [x:%f\n, y:%f\n, z:%f\n]",attitude.roll ,attitude.pitch, attitude.yaw);
	if (attitude.roll<0 && attitude.roll-lastRoll > 0.001) {
		NSLog(@"left rotation");
	}
	else if (attitude.roll>1.0 && attitude.roll-lastRoll > 0.001)
	{
		NSLog(@"right rotation");
	}
	lastRoll = attitude.roll;

}

//
//// Override to allow orientations other than the default portrait orientation.
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc 
{
	[glView release];
    [super dealloc];
}


@end
